package com.epoint.mobileoa.personalized.epointcompany.ui.gzrz;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.personalized.epointcompany.model.ShenHeInfoModel;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.StringHelp;

/**
 * @author liyc
 * @time 2012-8-13 下午2:49:13
 * @annotation 日志审核列表
 */
public class RizhiShenHeListView extends SuperView {

	private ListView lv;

	private int taskID;//1获取服务器本月第一天和最后一天时间字符串
	private List<ShenHeInfoModel> shenHeList = new ArrayList<ShenHeInfoModel>();
	private SHInfoAdapter adapter;
	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);
		adapter = new SHInfoAdapter();
		addFoot();
		lv.setAdapter(adapter);
		getServerDateTask();
	}

	private LinearLayout foot;
	public void addFoot(){
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	private void getServerDateTask(){
		taskID = 1;
		showDialog_wait("正在获取服务器日期...");
		Task task = new GetTheMonthFirstDayAndLastDayTask(this,null);
		MainService.addTask(task);
	}

	private void getShenHeInfoTask(String firstDay,String lastDay)
	{
		taskID = 2;
		showDialog_wait(null);
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("firstDay", firstDay);
		param.put("lastDay", lastDay);
		Task task = new GetRizhiShenHeInfoListTask(this, param);
		MainService.addTask(task);
	}

	private String passFirstDay;
	private String passLastDay;
	@Override
	public void refresh(Object... param) {
		m_Dialog.dismiss();
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskID==1)
			{
				String bs = param[1].toString();
				passFirstDay = StringHelp.getXMLAtt(bs, "firstDay");
								passLastDay = StringHelp.getXMLAtt(bs, "lastDay");
//				passLastDay = StringHelp.getXMLAtt(bs, "nowDate");
				getShenHeInfoTask(passFirstDay, passLastDay);
			}
			else if(taskID==2)
			{
				String bs = param[1].toString();
				List<ShenHeInfoModel> templist = GetRizhiShenHeInfoListTask.XmlAnalysis(bs);
				for(int i=templist.size()-1;i>=0;i--){
					shenHeList.add(templist.get(i));
				}
				adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.rizhishenhelistview;
	}

	@Override
	public String setTitle() {
		return "日志审核";
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		if(position==shenHeList.size()){
			//			getShenHeInfoTask(firstDay, lastDay);
			Calendar cal = Calendar.getInstance();  
			String[] dates = passFirstDay.split("-");
			cal.set(Calendar.YEAR, Integer.parseInt(dates[0]));
			cal.set(Calendar.MONTH, Integer.parseInt(dates[1])-1);
			cal.add(Calendar.MONTH, -1);
			int maxdate = cal.getActualMaximum(Calendar.DATE);
			passFirstDay = DateHelp.convertDate(cal.getTime(), "yyyy-MM")+"-01";
			passLastDay  = DateHelp.convertDate(cal.getTime(), "yyyy-MM")+"-"+maxdate;
			getShenHeInfoTask(passFirstDay, passLastDay);
			return;
		}
		else{
			Intent it = new Intent(this,RizhiShenHeDetailView.class);
			String datex = DateHelp.getFormatedDate(shenHeList.get(position).RZDate.split(" ")[0], "/");
			it.putExtra("rzdate", datex);
			startActivity(it);
		}
	}
	class SHInfoAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return shenHeList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			LayoutInflater mInflater = LayoutInflater.from(RizhiShenHeListView.this);
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.rizhishenhelistlayout, null);
				holder.iv = (ImageView) convertView
						.findViewById(R.id.iv);
				holder.tv = (TextView) convertView
						.findViewById(R.id.tv);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.iv.setVisibility(View.GONE);
			ShenHeInfoModel s = shenHeList.get(position);
			String CheckStatusStr = "";


			if(s.CheckStatus.equals("1"))
			{
				CheckStatusStr = "<font color="+getYGColor()+">无日志记录</font>";
			}
			else if(s.CheckStatus.equals("2"))
			{
				CheckStatusStr = "<font color=red>未审核</font>";
			}
			else if(s.CheckStatus.equals("3"))
			{
				CheckStatusStr = "<font color="+getBlackOrWhiteColor()+">"+"已审核"+"</font>";
				holder.iv.setVisibility(View.VISIBLE);
				holder.iv.setBackgroundResource(R.drawable.img_right_50);
			}
			else if(s.CheckStatus.equals("4"))
			{
				CheckStatusStr = "<font color=blue>部分审核</font>";
			}
			String datey = s.RZDate.split(" ")[0];

			String datex = DateHelp.getFormatedDate(datey, "/");
			String weekstr = DateHelp.getWeekByFormatedDateStr_HTML_Color(datex);

			String allInfo = "<font size=3 color="+getBlackOrWhiteColor()+"><big>"+datex+"("+weekstr+")</big></font>" +
					"<br>"+CheckStatusStr+"<br/>";

			if(!s.CheckStatus.equals("1"))
			{
				//allInfo += ""+s.UnCheckUserCount+"个人未审核,"+s.UnAddUserCount+"个人未填写";
				allInfo += "<font color="+getBlackOrWhiteColor()+">";
				if(!s.CheckUserCount.equals("0"))
				{
					allInfo += s.CheckUserCount+"人已审核,";
				}
				if(!s.UnCheckUserCount.equals("0")){
					allInfo += s.UnCheckUserCount+"人未审核,";
				}
				if(!s.UnAddUserCount.equals("0"))
				{
					allInfo += s.UnAddUserCount+"人未填写,";
				}
				if(!s.BuTianUserCount.equals("0"))
				{
					allInfo += s.BuTianUserCount+"人补填,";
				}
				allInfo += "</font>";
			}

			holder.tv.setText(Html.fromHtml(allInfo));

			return convertView;
		}
	}

	public class ViewHolder {
		public ImageView iv;
		public TextView tv;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}
